package daos.user.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import model.entities.User;
import daos.AbstractDAO;
import daos.db.Conexion;
import daos.user.UserDAO;

public class UserDAOImpl extends AbstractDAO implements UserDAO{
	
	Conexion con = new Conexion();

	public void insert(User user) throws SQLException {
		
		String insertSql = "INSERT INTO USERS (NAME, LOGIN, PASSWORD) VALUES (?,?,?)";
		PreparedStatement stmt = null;
		Connection conex = null;
		try {

			conex = con.getConnected();
			stmt = conex.prepareStatement(insertSql);
			
			stmt.setString(1, user.getLogin());
			stmt.setString(2, user.getName());
			stmt.setString(3, user.getPassword());

			System.out.println(insertSql);

			stmt.executeUpdate();
			System.out.println("Row inserted OK");

		} finally {
			closeConnection(conex, stmt, null);
		}
	}
	
	public void delete(User user) throws SQLException {
		String deleteSql = "DELETE FROM USERS WHERE LOGIN = ?";
		PreparedStatement stmt = null;
		Connection conex = null;
		try {

			conex = con.getConnected();
			stmt = conex.prepareStatement(deleteSql);
			
			stmt.setString(1, user.getLogin());

			System.out.println(deleteSql);

			stmt.executeUpdate();
			System.out.println("Row deleted OK");

		} finally {
			closeConnection(conex, stmt, null);
		}
	}
	
	public void select(User user) throws SQLException {
		String selectSql = "SELECT * FROM USERS WHERE LOGIN = ?";
		PreparedStatement stmt = null;
		Connection conex = null;
		try {

			conex = con.getConnected();
			stmt = conex.prepareStatement(selectSql);
			
			stmt.setString(1, user.getLogin());

			System.out.println(selectSql);

			stmt.executeUpdate();
			System.out.println("Row selected OK");

		} finally {
			closeConnection(conex, stmt, null);
		}
	}
}
